package com.chexiehui.jc.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.app.ActivityOptionsCompat;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.aries.ui.view.title.TitleBarView;
import com.bigkoo.svprogresshud.SVProgressHUD;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.chexiehui.jc.R;
import com.chexiehui.jc.entity.UserInfo;
import com.chexiehui.jc.utils.NoDoubleClickListener;
import com.chexiehui.jc.utils.SerializableSpTools;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import es.dmoral.toasty.Toasty;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;


/**
 * activity 基类
 * createBy JunSong
 */
public abstract class BaseActivity extends RxAppCompatActivity {

    protected TitleBarView titleBar;
    protected Activity mContext;
    protected boolean mIsFirstShow = true;
    private Unbinder mUnBinder;
    protected int type = 0;
    protected View mContentView;
    protected boolean isTablet;

    //private Toast toast;
    protected SVProgressHUD mSVProgressHUD;
    private List<BaseFragment> registFragmentList;
    private UserInfo userInfo;

    protected abstract void setTitleBar();

    //是否显示titleBar 底部线
    protected boolean isShowLine() {
        return false;
    }

    @LayoutRes
    protected abstract int getLayout();

    protected void beforeSetView() {
    }

    protected abstract void initView(Bundle var1);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.mContext = this;
        isTablet = isTablet();
        this.beforeSetView();
        mContentView = View.inflate(mContext, getLayout(), null);
        this.setContentView(mContentView);
        mUnBinder = ButterKnife.bind(this);
        mSVProgressHUD = new SVProgressHUD(this);
        userInfo = SerializableSpTools.getUserInfo();
        initTitle();
        this.initView(savedInstanceState);
        if (isRegisterEventBus()) {
            EventBus.getDefault().register(this);
        }
    }

    protected void initTitle() {
        titleBar = findViewById(R.id.titleBar);
        if (titleBar == null) {
            return;
        }

        titleBar.setStatusBarLightMode(true)
                .setOnLeftTextClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View view) {
                        onBackPressed();
                    }
                });
        setTitleBar();
    }

    /**
     * 是否注册事件分发
     *
     * @return true绑定EventBus事件分发，默认不绑定，子类需要绑定的话复写此方法返回true.
     */
    protected boolean isRegisterEventBus() {
        return false;
    }

    //设置是否冲底部关闭
    protected boolean isFinishToBottom() {
        return false;
    }

    public void startActivity(Intent intent) {
        startActivity(intent, R.anim.slide_in_from_right, R.anim.slide_out_to_left);
    }

    public void startActivity(Intent intent, int enterAnim, int exitAnim) {
        ActivityUtils.startActivity(intent, enterAnim, exitAnim);
    }

    public void startActivity(Context mContext, Class<? extends Activity> activity, Bundle bundle, int enterAnim, int exitAnim) {
        Intent intent = new Intent(mContext, activity);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        ActivityUtils.startActivity(intent, enterAnim, exitAnim);
    }

    public void startActivity(Class<? extends Activity> activity, Bundle bundle) {
        startActivity(mContext, activity, bundle, R.anim.slide_in_from_right, R.anim.slide_out_to_left);
    }

    public void startActivityForResult(Class<? extends Activity> activity, int requestCode, Bundle bundle, int enterAnim, int exitAnim) {
        Intent intent = new Intent(mContext, activity);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode, ActivityOptionsCompat.makeCustomAnimation(mContext, enterAnim, exitAnim).toBundle());
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            overridePendingTransition(enterAnim, exitAnim);
        }
    }

    public void startActivityForResult(Class<? extends Activity> activity, int requestCode, Bundle bundle) {
        startActivityForResult(activity, requestCode, bundle, R.anim.slide_in_from_right, R.anim.slide_out_to_left);
    }

    public void startActivityForResult(Class<? extends Activity> activity, int requestCode) {
        startActivityForResult(activity, requestCode, null, R.anim.slide_in_from_right, R.anim.slide_out_to_left);
    }

    public void startActivityNoAnim(Class<? extends Activity> activity, Bundle bundle) {
        startActivity(mContext, activity, bundle, 0, 0);
    }

    public void startActivity(Class<? extends Activity> activity) {
        startActivity(mContext, activity, null, R.anim.slide_in_from_right, R.anim.slide_out_to_left);
    }

    public void startActivityAnimBottom(Class<? extends Activity> activity) {
        startActivity(mContext, activity, null, R.anim.bottom_in, R.anim.bottom_silent);
    }

    public void startActivityAnimBottom(Class<? extends Activity> activity, Bundle bundle) {
        startActivity(mContext, activity, bundle, R.anim.bottom_in, R.anim.bottom_silent);
    }

    public void startActivityAnimBottomForResult(Class<? extends Activity> activity, int requestCode) {
        startActivityForResult(activity, requestCode, null, R.anim.bottom_in, R.anim.bottom_silent);
    }

    public void startActivityAnimBottomForResult(Class<? extends Activity> activity, int requestCode, Bundle bundle) {
        startActivityForResult(activity, requestCode, bundle, R.anim.bottom_in, R.anim.bottom_silent);
    }

    public View getRootView() {
        return ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
    }

    public void registFragment(BaseFragment fragment) {
        if (registFragmentList == null) {
            registFragmentList = new ArrayList<>();
        }
        registFragmentList.add(fragment);
    }

    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            boolean isFragmentsCancelRequestOnClickBack = false;
            if (registFragmentList != null && registFragmentList.size() > 0) {
                for (int i = 0; i < registFragmentList.size(); i++) {
                    boolean temp = registFragmentList.get(i).onKeyDown(keyCode, event);
                    if (temp) isFragmentsCancelRequestOnClickBack = true;
                }
            }
            if (isCancelRequestOnClickBack()) {
                if (mSVProgressHUD != null && mSVProgressHUD.isShowing()) {
                    mSVProgressHUD.dismiss();
                    return true;
                }
            }
            if (isFragmentsCancelRequestOnClickBack) {
                return true;
            }
            //点击两次退出登录判断
            if (isActivityExitAppTips()) {
                if ((System.currentTimeMillis() - exitTime) > 2000) {
                    showToast("再按一次将退出");
                    exitTime = System.currentTimeMillis();
                } else {
                    AppUtils.exitApp();
                }
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 点击返回取消正在请求的对话框
     *
     * @return
     */
    public boolean isCancelRequestOnClickBack() {
        return true;
    }

    /**
     * activity是否具有点击返回按钮提示以后退出app提示
     *
     * @return
     */
    public boolean isActivityExitAppTips() {
        return false;
    }


    @Override
    protected void onDestroy() {
        /*if (toast != null) {
            toast.cancel();
            toast = null;
        }*/
        if (mSVProgressHUD != null) {
            if (mSVProgressHUD.isShowing()) mSVProgressHUD.dismiss();
        }
        if (isRegisterEventBus() && EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        if (mUnBinder != null) {
            mUnBinder.unbind();
        }
        super.onDestroy();
        if (registFragmentList != null) {
            registFragmentList.clear();
            registFragmentList = null;
        }
        titleBar = null;
        mContext = null;
        mUnBinder = null;
        mContentView = null;
        //toast = null;
        mSVProgressHUD = null;
    }

    public List<BaseFragment> getRegisterFragmentList() {
        return registFragmentList;
    }

    @Override
    protected void onResume() {
        if (this.mIsFirstShow) {
            this.mIsFirstShow = false;
        }
        super.onResume();
    }

    @Override
    public void onBackPressed() {
        finish();
        if (!isFinishToBottom()) {
            overridePendingTransition(R.anim.slide_in_from_left, R.anim.slide_out_to_right);
        } else {
            overridePendingTransition(R.anim.bottom_silent, R.anim.bottom_out);
        }
    }

    public void onBackPressedDelayed(long time, Object result) {
        Observable.just(result)
                .delay(time, TimeUnit.MILLISECONDS)
                .compose(bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<Object>() {

                    @Override
                    protected void onStart() {
                        super.onStart();
                    }

                    @Override
                    public void onNext(@NonNull Object o) {
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                        onBackPressed();
                    }
                });
    }

    public void onBackFinish(long time, Object result) {
        Observable.just(result)
                .delay(time, TimeUnit.MILLISECONDS)
                .compose(bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<Object>() {

                    @Override
                    protected void onStart() {
                        super.onStart();
                        LogUtils.e("onBackFinish-->onStart");
                    }

                    @Override
                    public void onNext(@NonNull Object o) {
                        LogUtils.e("onBackFinish-->onNext");
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        LogUtils.e("onBackFinish-->onError");
                        //hide(4,"清除失败！");
                    }

                    @Override
                    public void onComplete() {
                        LogUtils.e("onBackFinish-->onComplete");
                        hide();
                        finish();
//                        onBackPressed();
                    }
                });
    }

    public void showToast(int resId) {
        showToast(getResources().getString(resId));
    }

    public void showToast(String text) {
        /*if (toast != null) {
            toast.cancel();
            toast = null;
        }
        ToastUtils.showShort(text);*/
        Toasty.normal(mContext, text).show();
    }

    public void showLongToast(String text) {
        /*if (toast != null) {
            toast.cancel();
            toast = null;
        }
        ToastUtils.showLong(text);*/
        Toasty.normal(mContext, text, Toast.LENGTH_LONG).show();
    }

    public void showDefault() {//
        if (mSVProgressHUD != null) {
            mSVProgressHUD.showWithMaskType(SVProgressHUD.SVProgressHUDMaskType.Black);
        }
    }

    public void showLoading(String loadding) {//1
        mSVProgressHUD.showWithStatus(StringUtils.isTrimEmpty(loadding) ? "加载中..." : loadding, SVProgressHUD.SVProgressHUDMaskType.Black);
    }

    public void showWarning(String warning) {//2
        if (mSVProgressHUD != null) {
            mSVProgressHUD.showInfoWithStatus(StringUtils.isTrimEmpty(warning) ? "警告" : warning, SVProgressHUD.SVProgressHUDMaskType.Black);
        }
    }

    public void showSuccess(String success) {//3
        mSVProgressHUD.showSuccessWithStatus(StringUtils.isTrimEmpty(success) ? "成功!" : success, SVProgressHUD.SVProgressHUDMaskType.Black);
    }

    public void showError(String error) {//4
        if (mSVProgressHUD != null) {
            mSVProgressHUD.showErrorWithStatus(StringUtils.isTrimEmpty(error) ? "出错啦～" : error, SVProgressHUD.SVProgressHUDMaskType.Black);
        }
    }

    public void showProgress(int progress) {//5
        if (mSVProgressHUD != null) {
            mSVProgressHUD.getProgressBar().setProgress(progress);
            mSVProgressHUD.showWithProgress("进度 " + progress + "%", SVProgressHUD.SVProgressHUDMaskType.Black);
        }
    }

    public void setProgressValue(int progress) {
        if (mSVProgressHUD != null) {
            if (mSVProgressHUD.getProgressBar().getMax() != mSVProgressHUD.getProgressBar().getProgress()) {
                mSVProgressHUD.getProgressBar().setProgress(progress);
                mSVProgressHUD.setText("进度 " + progress + "%");
            }
        }
    }

    public void hide() {
        if (mSVProgressHUD != null && mSVProgressHUD.isShowing()) {
            mSVProgressHUD.dismiss();
        }
    }

    public void removeCallBack(ApiCallback callback) {

    }

    public Context getMContext() {
        return mContext;
    }


    /**
     * 设置是否点击屏幕关闭软键盘
     */
    protected boolean isDispatchTouchHideSoftInput() {
        return true;
    }

    /**
     * 重写系统事件分发
     *
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN && isDispatchTouchHideSoftInput()) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    /**
     * 返回软键盘是否打开
     *
     * @param v
     * @param event
     * @return
     */
    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }


    public boolean isLogin() {
        return false;
    }

    public String getStringSource(int res) {
        return getString(res);
    }

    public String getUserToken() {
        return "";
    }

    public String getUserId() {
        return userInfo.getId();
    }

    public void gotoLogin() {
        /*startActivityAnimBottom(LoginActivity.class,
                new BundleBuilder().putInt("comeInType", 1).create());*/
    }


    public boolean isFinish() {
        boolean a = mContext == null || mContext.isFinishing();
        if (Build.VERSION.SDK_INT < 17) {
            return a;
        } else {
            return a || mContext.isDestroyed() || mContext.getFragmentManager().isDestroyed();
        }
    }

    public boolean isTablet(){
        if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE){
            return true;
        }else{
            return false;
        }
    }
}
